<?php

class ThongKeKiemKeController {

    public function invoke() {
        $dir = dirname(__FILE__);

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }

        // tác vụ
        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }

        if ($action == 'thongketuan') {


            $idbep = $_POST['BEP_ID'];
            $thang = $_POST['THANG'];
            $tuan = $_POST['TUAN'];
            $beps = $this->getBepId($idbep);




             //Trường hợp tháng =1 tuần =1
             if(($thang==1) && ($tuan==1)){
                      // echo 'a';exit();       
                $results = $this->showdulieu($idbep, $thang, $tuan);
                if ($results->getKK_TONG_KHO() != null) {
                            
                $dulieu = ' <thead>
                <tr  class="ui-bar-d">
                <th data-priority="5">Bếp</th>
                          <th data-priority="3">Tuần</th>
                          <th data-priority="5">Tháng</th>
                           <th data-priority="5">Từ ngày đến ngày</th>
                          <th data-priority="1">Tổng tiền</th>
                </tr>
                </thead>
                <tbody>
                   <tr>
                    <td>' . $beps->getBEP_TEN_BEP() . '</td>
                     <td>' . $tuan . '</td>
                     <td>' . $thang . '</td>
                    <td>' . $results->getMAX_KK_NGAY_KIEM_KE_BEGIN() . ' - ' . $results->getMIN_KK_NGAY_KIEM_KE_BEGIN() . '</td>     
                     <td>' . Utility::formatToCurrencyString($results->getKK_TONG_KHO()) . ' (VNĐ)</td>
                   </tr>
       
                </tbody>';
            } else {
                $dulieu = '<thead>
                <tr  class="ui-bar-d">
                        <th data-priority="5">Bếp</th>
                          <th data-priority="3">Tuần</th>
                          <th data-priority="5">Tháng</th>
                           <th data-priority="5">Từ ngày đến ngày</th>
                          <th data-priority="1">Tổng tiền</th>
                </tr>
            </thead> ';
            }
            echo $dulieu;
            exit();
                        
            }
            //Kết thúc trường hợp tháng =1 tuần =1


            //Trường hợp tháng >1 tuần >=1
            else {




                $tuan_=$tuan-1;
                $thang_=$thang-1;
                if( $tuan_==0 && ($thang>1)){
                    $tongtien=0;
                    $tongtientuannay=0;
                    $tongtientuantruoc=0;

                    $results_ = $this->showdulieu($idbep, $thang_, 4);
                    $tongtientuantruoc=$results_->getKK_TONG_KHO();

                    $results = $this->showdulieu($idbep, $thang, $tuan);
                    $tongtientuannay=$results->getKK_TONG_KHO();

                    $tongtien=$tongtientuantruoc-$tongtientuannay;

                    if ($results->getKK_TONG_KHO() != null) {
                                    
                        $dulieu = ' <thead>
                        <tr  class="ui-bar-d">
                        <th data-priority="5">Bếp</th>
                                  <th data-priority="3">Tuần</th>
                                  <th data-priority="5">Tháng</th>
                                   <th data-priority="5">Từ ngày đến ngày</th>
                                  <th data-priority="1">Tổng tiền</th>
                        </tr>
                        </thead>
                        <tbody>
                           <tr>
                            <td>' . $beps->getBEP_TEN_BEP() . '</td>
                             <td>' . $tuan . '</td>
                             <td>' . $thang . '</td>
                            <td>' . $results->getMAX_KK_NGAY_KIEM_KE_BEGIN() . ' - ' . $results->getMIN_KK_NGAY_KIEM_KE_BEGIN() . '</td>     
                             <td>' . Utility::formatToCurrencyString($tongtien) . ' (VNĐ)</td>
                           </tr>
               
                        </tbody>';
                    } else {
                        $dulieu = '<thead>
                        <tr  class="ui-bar-d">
                                <th data-priority="5">Bếp</th>
                                  <th data-priority="3">Tuần</th>
                                  <th data-priority="5">Tháng</th>
                                   <th data-priority="5">Từ ngày đến ngày</th>
                                  <th data-priority="1">Tổng tiền</th>
                        </tr>
                        </thead> ';
                    }
                    echo $dulieu;
                    exit();

                    }else{
                        $tuan_=$tuan-1;
                        $tongtien=0;
                        $tongtientuannay=0;
                        $tongtientuantruoc=0;

                        $results_ = $this->showdulieu($idbep, $thang, $tuan_);
                        $tongtientuantruoc=$results_->getKK_TONG_KHO();

                        $results = $this->showdulieu($idbep, $thang, $tuan);
                        $tongtientuannay=$results->getKK_TONG_KHO();

                        $tongtien=$tongtientuantruoc-$tongtientuannay;  

                        if ($results->getKK_TONG_KHO() != null) {
                                        
                            $dulieu = ' <thead>
                            <tr  class="ui-bar-d">
                            <th data-priority="5">Bếp</th>
                                      <th data-priority="3">Tuần</th>
                                      <th data-priority="5">Tháng</th>
                                       <th data-priority="5">Từ ngày đến ngày</th>
                                      <th data-priority="1">Tổng tiền</th>
                            </tr>
                            </thead>
                            <tbody>
                               <tr>
                                <td>' . $beps->getBEP_TEN_BEP() . '</td>
                                 <td>' . $tuan . '</td>
                                 <td>' . $thang . '</td>
                                <td>' . $results->getMAX_KK_NGAY_KIEM_KE_BEGIN() . ' - ' . $results->getMIN_KK_NGAY_KIEM_KE_BEGIN() . '</td>     
                                 <td>' . Utility::formatToCurrencyString($tongtien) . ' (VNĐ)</td>
                               </tr>
                   
                            </tbody>';
                        } else {
                            $dulieu = '<thead>
                            <tr  class="ui-bar-d">
                                    <th data-priority="5">Bếp</th>
                                      <th data-priority="3">Tuần</th>
                                      <th data-priority="5">Tháng</th>
                                       <th data-priority="5">Từ ngày đến ngày</th>
                                      <th data-priority="1">Tổng tiền</th>
                            </tr>
                            </thead> ';
                        }
                        echo $dulieu;
                        exit();


                        }

                 }

    }








       





        if ($task == '') {
            $viewFile = $dir . '/views/thongkekiemke.php';
            $xtemplate = new XTemplate($viewFile);

            $idbep = '';
            $tenbep = '';
            $thang = 0;
            $tuan = 0;


            $maxthang = $this->getMaxThang();
            for ($k = 1; $k <= $maxthang->getTHANG_ID(); $k++) {

                if ($maxthang->getTHANG_ID() == $k) {
                    $xtemplate->assign('THANG', '<option selected="selected" value="' . $k . '">Tháng ' . $k . '</option>');
                    $thang = $k;
                } 
                else {
                    $xtemplate->assign('THANG', '<option value="' . $k . '">Tháng ' . $k . '</option>');
                }                   
                $xtemplate->parse('main.thang_');
            }

            $maxthang_ = $this->getMaxThang();
            for ($k = 1; $k <= $maxthang_->getTHANG_ID(); $k++) {

                if ($maxthang->getTHANG_ID() == $k) {
                    $xtemplate->assign('THANG', '<option selected="selected" value="' . $k . '">Tháng ' . $k . '</option>');
                    $thang = $k;
                } 
                else {
                    $xtemplate->assign('THANG', '<option value="' . $k . '">Tháng ' . $k . '</option>');
                }                   
                $xtemplate->parse('main.thang');
            }
            
            
            $maxtuan_thang = $this->getMaxTuan($maxthang->getTHANG_ID());
            for ($i = 1; $i <= $maxtuan_thang->getTUAN_ID(); $i++) {
                if ($maxtuan_thang->getTUAN_ID() == $i) {
                    $xtemplate->assign('TUAN', '<option selected="selected" value="' . $i . '">Tuần ' . $i . '</option>');
                    $tuan = $i;
                } else {
                    $xtemplate->assign('TUAN', '<option value="' . $i . '">Tuần ' . $i . '</option>');
                }
                $xtemplate->parse('main.tuan');
            }

            $beps = $this->getAllBep();
            $index = 1;
            if ($beps != null) {

                foreach ($beps as $bep) {

                    if ($index == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep');
                    $index++;
                }
            }

            $beps_ = $this->getAllBep();
            $index_ = 1;
            if ($beps_ != null) {

                foreach ($beps_ as $bep) {

                    if ($index_ == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep_');
                    $index_++;
                }
            }
            
            $beps__ = $this->getAllBep();
            $index__ = 1;
            if ($beps__ != null) {

                foreach ($beps__ as $bep) {

                    if ($index__ == 1) {
                        $idbep = $bep->getBEP_ID();
                        $tenbep = $bep->getBEP_TEN_BEP();
                    }
                    $xtemplate->assign('BEP_ID', $bep->getBEP_ID());
                    $xtemplate->assign('BEP_TEN_BEP', $bep->getBEP_TEN_BEP());
                    $xtemplate->parse('main.bep__');
                    $index__++;
                }
            }
            
            
           


            if(($thang==1) && ($tuan==1)){
                $results = $this->showdulieu($idbep, $thang, $tuan);
                if ($results->getKK_TONG_KHO() != null) {
                            $xtemplate->assign('BEP_ID', $tenbep);
                            $xtemplate->assign('THANG', $thang);
                            $xtemplate->assign('TUAN', $tuan);
                            $xtemplate->assign('TUNGAY', $results->getMIN_KK_NGAY_KIEM_KE_BEGIN());
                            $xtemplate->assign('DENNGAY', $results->getMAX_KK_NGAY_KIEM_KE_BEGIN());
                            $xtemplate->assign('TONGTIEN', Utility::formatToCurrencyString($results->getKK_TONG_KHO()));

                            $xtemplate->parse('main.thongke');
                        }
            }
            else {
                $tuan_=$tuan-1;
                $thang_=$thang-1;
                if( $tuan_==0 && ($thang>1)){
                $tongtien=0;
                $tongtientuannay=0;
                $tongtientuantruoc=0;

                $results_ = $this->showdulieu($idbep, $thang_, 4);
                $tongtientuantruoc=$results_->getKK_TONG_KHO();

                $results = $this->showdulieu($idbep, $thang, $tuan);
                $tongtientuannay=$results->getKK_TONG_KHO();

                $tongtien=$tongtientuantruoc-$tongtientuannay;

                if ($results->getKK_TONG_KHO() != null) {
                            $xtemplate->assign('BEP_ID', $tenbep);
                            $xtemplate->assign('THANG', $thang);
                            $xtemplate->assign('TUAN', $tuan);
                            $xtemplate->assign('TUNGAY', $results->getMIN_KK_NGAY_KIEM_KE_BEGIN());
                            $xtemplate->assign('DENNGAY', $results->getMAX_KK_NGAY_KIEM_KE_BEGIN());
                            $xtemplate->assign('TONGTIEN', Utility::formatToCurrencyString($tongtien));

                            $xtemplate->parse('main.thongke');
                        }

                }else{
                $tuan_=$tuan-1;
                $tongtien=0;
                $tongtientuannay=0;
                $tongtientuantruoc=0;

                $results_ = $this->showdulieu($idbep, $thang, $tuan_);
                $tongtientuantruoc=$results_->getKK_TONG_KHO();

                $results = $this->showdulieu($idbep, $thang, $tuan);
                $tongtientuannay=$results->getKK_TONG_KHO();

                $tongtien=$tongtientuantruoc-$tongtientuannay;

                if ($results->getKK_TONG_KHO() != null) {
                            $xtemplate->assign('BEP_ID', $tenbep);
                            $xtemplate->assign('THANG', $thang);
                            $xtemplate->assign('TUAN', $tuan);
                            $xtemplate->assign('TUNGAY', $results->getMIN_KK_NGAY_KIEM_KE_BEGIN());
                            $xtemplate->assign('DENNGAY', $results->getMAX_KK_NGAY_KIEM_KE_BEGIN());
                            $xtemplate->assign('TONGTIEN', Utility::formatToCurrencyString($tongtien));

                            $xtemplate->parse('main.thongke');
                        }
                }

            }


           

           


            $xtemplate->parse('main');
            $xtemplate->out('main');
        }
    }

   

    private function getMaxThang() {
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->getMaxThang($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getMaxTuan($idthang) {
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $kiemke->setTHANG_ID($idthang);
            
            $result = $kiemke->getMaxTuan($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function showdulieu($idbep, $thang, $tuan) {
        $db = new Database();
        try {
           $kiemke= new Kiem_ke();

            $result = $kiemke->showdulieu($idbep, $thang, $tuan, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function showdulieu_thang($idbep, $thang) {
     
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->showdulieu_thang($idbep, $thang, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

     private function showdulieu_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY) {
     
        $db = new Database();
        try {
            $kiemke= new Kiem_ke();

            $result = $kiemke->showdulieu_tungay_denngay($BEP_ID, $TUNGAY,$DENNGAY, $db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }
    
    private function getAllBep() {
        $db = new Database();
        try {
            $bep = new Bep();

            $result = $bep->getList($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getBepId($id) {
        $db = new Database();
        try {
            $bep = new Bep();
            $bep->setBEP_ID($id);
            $result = $bep->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

}
